#include "CPlayerFallState.h"
#include "CPlayer.h"
#include "CPlayerIdleState.h"
#include "CPlayerWalkState.h"
#include "CTimer.h"
#include "CSGD_DirectInput.h"

#define GRAVITY 20

CPlayerFallState::CPlayerFallState()
{
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Greek_Jump));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(MidEvil_Jump));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Gothic_Jump));
}

CPlayerFallState *CPlayerFallState::GetInstance()
{
	static CPlayerFallState Instance;
	return &Instance;
}

void CPlayerFallState::Enter(CPlayer *pPlayer)
{
	m_pPlayer = pPlayer;
	m_pPlayer->m_bFalling = true;
}

void CPlayerFallState::Run()
{
	//m_vAnimations[m_pPlayer->m_unTimePeriod]->Play(CTimer::GetInstance()->GetElapsedTime());

	bool bWalking = false;

	Vec3 DownVec = -m_pPlayer->m_Pos;
	DownVec.Normalize();

	Vec3 vFormerGravity = Dot(DownVec, m_pPlayer->m_Vel) * DownVec;

	CSGD_DirectInput * di = CSGD_DirectInput::GetInstance();
	if(di->KeyDown(DIK_RIGHTARROW))
	{
		m_pPlayer->m_bFlipped = false;
		bWalking = true;
		Vec3 Right(m_pPlayer->m_Pos.y, -m_pPlayer->m_Pos.x, 0);
		Right.Normalize();
		Right *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Right + vFormerGravity;					
	}													
	else if (di->KeyDown(DIK_LEFTARROW))
	{
		m_pPlayer->m_bFlipped = true;
		bWalking = true;
		Vec3 Left(-m_pPlayer->m_Pos.y, m_pPlayer->m_Pos.x, 0);
		Left.Normalize();
		Left *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Left + vFormerGravity;
	}
	
// 	if((Dot(DownVec, m_pPlayer->m_Vel) > 0.0f))
// 	{		
// 		if (!bWalking)
// 			m_pPlayer->ChangeState(	CPlayerIdleState::GetInstance());
// 		else
// 			m_pPlayer->ChangeState( CPlayerWalkState::GetInstance());
// 	}
	
	DownVec *= GRAVITY;		//Gravity Constant
	m_pPlayer->m_Vel += DownVec * CTimer::GetInstance()->GetElapsedTime();
}

void CPlayerFallState::Exit()
{
	m_pPlayer->m_bFalling = false;
}